package cn.edu.hunau.controller;

import cn.edu.hunau.pojo.PageResult;
import cn.edu.hunau.pojo.Product;
import cn.edu.hunau.pojo.ProductResult;
import cn.edu.hunau.service.ProductService;
import cn.edu.hunau.service.impl.ProductServiceImpl;
import cn.edu.hunau.utils.JsonUtils;
import cn.edu.hunau.utils.R;
import cn.edu.hunau.utils.ResultCodeEnum;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @Author 苏佳
 * @Date 2022 12 12 09 27
 **/
@WebServlet("*.product")
public class ProductController extends HttpServlet {
    private ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String match = req.getHttpServletMapping().getMatchValue();
        PrintWriter out = resp.getWriter();
        if ("load".matches(match)){
            String idStr = req.getParameter("id");
            try {
                ProductResult<Map<String, Object>> productInfo = productService.loadProductByPid(Integer.parseInt(idStr.trim()));
                out.println(JsonUtils.objectToJson(R.ok().data(productInfo)));
            } catch (Exception e) {
                out.println(JsonUtils.objectToJson(R.error().message(e.getMessage())));
            }
        }
        else if ("page".equals(match)){
            //根据类别进行分页查询
            String cidStr = req.getParameter("cid");
            String pageIndexStr = req.getParameter("pageIndex");
            int pageSize=4;//默认页面大小为4条记录

            Integer cid = Integer.parseInt(cidStr.trim());
            Integer pageIndex = Integer.parseInt(pageIndexStr.trim());

            PageResult<Product> pageResult = productService.pageQueryByCid(cid, pageIndex, pageSize);
            out.println(JsonUtils.objectToJson(R.ok().data(pageResult)));
        }
        else if ("pageSearchByKeyWord".equals(match)){
            //根据关键词进行分页查询
            String keyWord = req.getParameter("keyWord");
            System.out.println(keyWord.trim());
            //逻辑的实现 : 对数据格式进行判断
            if(keyWord.length()==0){
                out.println(JsonUtils.objectToJson(R.setResult(ResultCodeEnum.USER_HASNOT_INPUT)));
            }else{
                String pageIndexStr = req.getParameter("pageIndex");
                int pageSize=4;//默认页面大小为4条记录

                Integer pageIndex = Integer.parseInt(pageIndexStr.trim());

                PageResult<Product> pageResult = productService.pageQueryByKeyWord(keyWord, pageIndex, pageSize);
                out.println(JsonUtils.objectToJson(R.ok().data(pageResult)));
            }

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);

    }
}
